<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta property="og:title" content="使用docker运行MongoDB数据库" />
<meta property="og:description" content="安装Docker 国内一键安装脚本
1 curl -sSL https://get.daocloud.io/docker | sh 卸载旧版本
1 2 3 4 5 6 7 8 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine 使用仓库安装
1 2 3 4 5 6 7 8 # 阿里云 sudo yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 清华镜像 sudo yum-config-manager \ --add-repo \ https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo 启动 Docker
1 sudo systemctl start docker 卸载
1 yum remove docker-ce 删除镜像、容器、配置文件等内容" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://wuju-i.gitee.io/post/20210714/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-07-14T00:00:00+00:00" />
<meta property="article:modified_time" content="2021-07-14T00:00:00+00:00" />

  <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="使用docker运行MongoDB数据库"/>
<meta name="twitter:description" content="安装Docker 国内一键安装脚本
1 curl -sSL https://get.daocloud.io/docker | sh 卸载旧版本
1 2 3 4 5 6 7 8 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine 使用仓库安装
1 2 3 4 5 6 7 8 # 阿里云 sudo yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 清华镜像 sudo yum-config-manager \ --add-repo \ https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo 启动 Docker
1 sudo systemctl start docker 卸载
1 yum remove docker-ce 删除镜像、容器、配置文件等内容"/>

  
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#262d33">
  <title>
    
    橘子的博客 - 使用docker运行MongoDB数据库
    
  </title>
  
  <link rel="shortcut icon" href="g.png" type="image/x-icon" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&display=swap"
    rel="stylesheet">
  
  
  
  <link rel="stylesheet" href="/minima.1714031344.css">

  
  
  <script defer type="text/javascript" src="/minima.1714031344.js"></script>
</head>
<script>
  
  let theme_2b_used = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

  try {
    if (!('theme' in localStorage)) {
      const default_theme = '';
      
      
      if (default_theme === 'dark' || default_theme === 'light') {
        theme_2b_used = default_theme;
      }

      
      localStorage.theme = theme_2b_used;
    }
    
    document.querySelector('html').classList.add(localStorage.theme);
  } catch (e) {
    console.error(e);
  }
</script>


<body class=" sm:mx-5 sm:my-0">
  <header class="flex justify-between items-center mb-6 sm:my-3">
  <div class="flex items-center">
    
    <div id="theme-switcher" class="text-4xl cursor-pointer">🌑</div>
  </div>
  <nav class="flex items-center
    whitespace-nowrap overflow-x-auto overflow-y-hidden">
    
    <a class="ml-5" href="/">首页</a>
    
    <a class="ml-5" href="/categories">分类</a>
    
    <a class="ml-5" href="/log">日记</a>
    
    <a class="ml-5" href="/about">关于</a>
    
  </nav>
</header>
  
  

<h1 class="mt-6 mb-6">使用docker运行MongoDB数据库</h1>
<div class="mb-3 text-xs flex justify-between sm:flex-col">
	<div>
	
		Posted at &mdash; 21年 7月14日
	
		
	</div>
	
</div>
<main>
	<p></p>
	<article class="md">
		<h2 id="安装docker">安装Docker</h2>
<p>国内一键安装脚本</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">curl -sSL https://get.daocloud.io/docker <span class="p">|</span> sh
</span></span></code></pre></td></tr></table>
</div>
</div><p>卸载旧版本</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">sudo yum remove docker <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-client <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-client-latest <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-common <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-latest <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-latest-logrotate <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-logrotate <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>                  docker-engine
</span></span></code></pre></td></tr></table>
</div>
</div><p>使用仓库安装</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 阿里云</span>
</span></span><span class="line"><span class="cl">sudo yum-config-manager <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>    --add-repo <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
</span></span><span class="line"><span class="cl"><span class="c1"># 清华镜像</span>
</span></span><span class="line"><span class="cl">sudo yum-config-manager <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>    --add-repo <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
</span></span></code></pre></td></tr></table>
</div>
</div><p>启动 Docker</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">sudo systemctl start docker
</span></span></code></pre></td></tr></table>
</div>
</div><p>卸载</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">yum remove docker-ce
</span></span></code></pre></td></tr></table>
</div>
</div><p>删除镜像、容器、配置文件等内容</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">rm -rf /var/lib/docker
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="安装mongodb">安装MongoDB</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="c1"># 取最新版的 MongoDB 镜像</span>
</span></span><span class="line"><span class="cl"><span class="c1"># mongo:latest 有坑</span>
</span></span><span class="line"><span class="cl">docker pull mongo:4.4
</span></span><span class="line"><span class="cl">docker images
</span></span><span class="line"><span class="cl"><span class="c1"># 运行</span>
</span></span><span class="line"><span class="cl">docker run -itd --name mongo -p 27017:27017 mongo:4.4 --auth
</span></span><span class="line"><span class="cl">docker ps
</span></span><span class="line"><span class="cl">docker <span class="nb">exec</span> -it mongo mongo admin
</span></span><span class="line"><span class="cl"><span class="c1"># 创建一个名为 admin，密码为 123456 的用户。</span>
</span></span><span class="line"><span class="cl">&gt;  db.createUser<span class="o">({</span> user:<span class="s1">&#39;admin&#39;</span>,pwd:<span class="s1">&#39;123456&#39;</span>,roles:<span class="o">[</span> <span class="o">{</span> role:<span class="s1">&#39;userAdminAnyDatabase&#39;</span>, db: <span class="s1">&#39;admin&#39;</span><span class="o">}</span>,<span class="s2">&#34;readWriteAnyDatabase&#34;</span><span class="o">]})</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 尝试使用上面创建的用户信息进行连接。</span>
</span></span><span class="line"><span class="cl">&gt; db.auth<span class="o">(</span><span class="s1">&#39;admin&#39;</span>, <span class="s1">&#39;123456&#39;</span><span class="o">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div>
	</article>
</main>





	

	

	



  <footer class="mt-8 flex sm:flex-col-reverse justify-between items-center">
  <p class="mt-0 text-sm">
    © 2022 | 橘子
    
  </p>
  <p class="flex items-center mt-0">
    
  </p>
</footer>
</body>
</html>